package com.tbit.main.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tbit.main.common.JsonResult;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.main.constant.OperateLog;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.RecoveryLog;
import com.tbit.main.pojo.VO.RecoveryLogStat;
import com.tbit.main.service.MachineService;
import com.tbit.main.service.RecoveryLogService;
import com.tbit.main.service.TokenService;
import com.tbit.main.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 车辆回收管理
 *
 * @Author: chen
 * @Description: 回收车辆
 * @Date: 创建于 16:15 2023/2/9
 */
@RestController
@RequestMapping("/recoveryLog")
public class RecoveryLogController {

    @Autowired
    private RecoveryLogService recoveryLogService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private MachineService machineService;

    /**
     * 分页查询回收记录
     *
     * @param accountId 区域id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param pageNO    页码数
     * @param rowCount  每页大小
     * @param mapType   地图类型 0原始 1百度2谷歌、高德
     * @return
     */
    @RequestMapping("/queryPage")
    public JsonResult queryPage(Integer accountUserId, Integer accountId, Long startTime, Long endTime,
                                @RequestParam(defaultValue = "1") Integer pageNO, @RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "2") Integer mapType) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        Assert.notNull(startTime, "System.startTime.notNull");
        Assert.notNull(endTime, "System.endTime.notNull");
        Assert.notNull(accountUserId, "User.userId.notNull");
        Page<RecoveryLog> recoveryLogPage = recoveryLogService.queryPage(accountUserId, accountId, startTime, endTime, pageNO, rowCount, mapType);
        return JsonResult.succ((int) recoveryLogPage.getTotal(), recoveryLogPage.getRecords());
    }

    /**
     * 回收车辆
     *
     * @param userCode 车辆编号
     * @param token    鉴权秘钥
     * @return
     */
    @RequestMapping("/add")
    @OperateLog(module = ModuleEnum.RECOVERYLOG, methods = MethodEnum.RECOVERYLOG_ADD)
    public JsonResult add(String userCode, String token) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        recoveryLogService.add(userCode, accountUser);
        return JsonResult.succ();
    }

    /**
     * 取消回收
     *
     * @param userCode 车辆编号
     * @param token    鉴权秘钥
     * @return
     */
    @RequestMapping("/del")
    @OperateLog(module = ModuleEnum.RECOVERYLOG, methods = MethodEnum.RECOVERYLOG_EDIT)
    public JsonResult del(String userCode, String token) {
        Assert.hasLength(userCode, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        recoveryLogService.del(userCode, accountUser);
        return JsonResult.succ();
    }

    /**
     * 批量回收
     *
     * @param userCodes 车辆编号
     * @param token     鉴权秘钥
     */
    @RequestMapping("/addBatch")
    @OperateLog(module = ModuleEnum.RECOVERYLOG, methods = MethodEnum.RECOVERYLOG_ADD)
    public JsonResult addBatch(@RequestParam("userCodes") List<String> userCodes, String token) {
        Assert.notNull(userCodes, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        recoveryLogService.addBatch(userCodes, accountUser);
        return JsonResult.succ();
    }

    /**
     * 批量取消
     *
     * @param userCodes 车辆编号
     * @param token     鉴权秘钥
     */
    @RequestMapping("/delBatch")
    @OperateLog(module = ModuleEnum.RECOVERYLOG, methods = MethodEnum.RECOVERYLOG_EDIT)
    public JsonResult delBatch(@RequestParam("userCodes") List<String> userCodes, String token) {
        Assert.notNull(userCodes, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        recoveryLogService.delBatch(userCodes, accountUser);
        return JsonResult.succ();
    }

    /**
     * 分组统计回收车辆
     *
     * @param accountId 区域id
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     */
    @RequestMapping("/getUserRecoveryLogNum")
    public JsonResult getUserRecoveryLogNum(Integer accountId, Long startTime,
                                            Long endTime) {
        Assert.notNull(accountId, "AccountUser.agentId.notNull");
        Assert.notNull(startTime, "System.startTime.notNull");
        Assert.notNull(endTime, "System.endTime.notNull");
        List<RecoveryLogStat> recoveryLogStats = recoveryLogService.getUserRecoveryLogNum(accountId, startTime, endTime);
        return JsonResult.succ(recoveryLogStats);
    }

    /**
     * 通过编号获取批量回收编号
     *
     * @param userCode 车辆编号
     * @param token    鉴权秘钥
     * @return
     */
    @RequestMapping("/getByUserCode")
    public JsonResult getByUserCode(String userCode, String token,Integer accountId) {
        Assert.notNull(userCode, "Machine.userCode.notNull");
        AccountUser accountUser = tokenService.getAccount(token);
        List<String> list = recoveryLogService.getByUserCode(userCode, accountUser,accountId);
        //r如果回收表里面没有直接添加当前车辆
        if (list == null || list.size() == 0) {
            machineService.authCheck(accountUser, userCode, accountId);
            list.add(userCode);
        }
        return JsonResult.succ(list);
    }
}
